package other;

import java.util.Arrays;

public class reverseStr_541 {
    public static String reverseStr(String s, int k) {
        char[] arr = s.toCharArray();
        for (int i = 0; i < arr.length; i += 2 * k) {
            int l = i;
            int r=Math.min(arr.length-1,i+k-1);  //避免剩余够不够 k 个
            reverse(arr, l, r);
        }
        return String.valueOf(arr);
    }

    public static void reverse(char[] arr, int l, int r) {
        while (l < r) {
            char temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;
            l++;
            r--;
        }
    }

    public static void main(String[] args) {
        String s = "abcdefg";
        int k = 2;
        System.out.println(reverseStr(s, k));
    }
}
